package com.iflytek.util;

import org.apache.commons.dbcp.BasicDataSource;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;


public class DbUtils {


    /**
     * 根据db.properties文件构造DataSource对象并返回
     * @return
     */
    public static DataSource getDataSource() {
        BasicDataSource basicDataSource = new BasicDataSource();
        Properties properties = new Properties();
        try {
            properties.load(DbUtils.class.getClassLoader().getResourceAsStream("db.properties"));
            String driver = properties.getProperty("driver");
            String url = properties.getProperty("url");
            String username = properties.getProperty("username");
            String password = properties.getProperty("password");
            Class.forName(driver);
            DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException|SQLException|IOException e) {
            e.printStackTrace();
        }
        basicDataSource.setDriverClassName(properties.getProperty("driver"));
        basicDataSource.setUrl(properties.getProperty("url"));
        basicDataSource.setUsername(properties.getProperty("username"));
        basicDataSource.setPassword(properties.getProperty("password"));
        return basicDataSource;
    }




}
